/*
 * Copyright (c) 2011, Michael Bedward
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.mousebender.gridfacade;

import org.geotools.geometry.jts.ReferencedEnvelope;

/**
 * Immutable data class to hold the bounds and cell dimensions of a {@linkplain GridView}.
 * 
 * @author michael
 */
public class GridViewParams {

    private final ReferencedEnvelope bounds;
    private final double cellXDim;
    private final double cellYDim;

    public GridViewParams(ReferencedEnvelope bounds, double cellXDim, double cellYDim) {
        this.bounds = new ReferencedEnvelope(bounds);
        this.cellXDim = cellXDim;
        this.cellYDim = cellYDim;
    }

    /**
     * Gets the bounding envelope. The returned instance is a copy.
     * 
     * @return the bounding envelope
     */
    public ReferencedEnvelope getBounds() {
        return new ReferencedEnvelope(bounds);
    }

    /**
     * Gets the grid cell X dimension.
     * 
     * @return cell X dimension
     */
    public double getCellXDim() {
        return cellXDim;
    }

    /**
     * Gets the grid cell Y dimension.
     * 
     * @return cell Y dimension
     */
    public double getCellYDim() {
        return cellYDim;
    }
    
    /**
     * Gets the grid view cell which contains the given location.
     * 
     * @param x location X ordinate
     * @param x location Y ordinate
     * 
     * @return the cell or {@linkplain GridViewCell#NOWHERE} if the location
     *     is not within bounds
     */
    public GridViewCell getCell(double x, double y) {
        double cellX = Double.NaN;
        double cellY = Double.NaN;
        boolean inside = true;
        
        double xdash = x - bounds.getMinX();
        if (xdash >= 0.0 && xdash < bounds.getWidth()) {
            int col = (int) Math.floor(xdash / cellXDim);
            cellX = bounds.getMinX() + col * cellXDim;
        } else {
            inside = false;
        }

        if (inside) {
            double ydash = y - bounds.getMinY();
            if (ydash >= 0.0 && ydash < bounds.getHeight()) {
                int row = (int) Math.floor(ydash / cellYDim);
                cellY = bounds.getMinY() + row * cellYDim;
            } else {
                inside = false;
            }
        }
        
        if (inside) {
            return new GridViewCell(cellX, cellY, cellXDim, cellYDim);
        } else {
            return GridViewCell.NOWHERE;
        }
    }

}
